package modulo3.model;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.nio.file.FileSystems;
import java.nio.file.Files;

/**
 *
 * @author jjsanche
 */
public class SerializacionDeRecordatorios {

    private static final String fileName = "Recordatorios.ser";

    public static void serializarRecordatorios(Recordatorios recordatorios) throws IOException {
        ObjectOutputStream outputStream = null;

        try {
            outputStream = new ObjectOutputStream(new FileOutputStream(fileName));
            outputStream.writeObject(recordatorios);
            outputStream.flush();
            outputStream.close();
        } catch (IOException ex) {
            System.err.println("Error al guardar los recordatorios.");
            System.err.println(ex.getMessage());

            if (outputStream != null) {
                outputStream.close();
            }
        }
    }

    public static Recordatorios desSerializarRecordatorios() {
        Recordatorios recordatorios = null;

        ObjectInputStream inputStream = null;

        try {
            if (Files.exists(FileSystems.getDefault().getPath(fileName))) {
                inputStream = new ObjectInputStream(new FileInputStream(fileName));
                recordatorios = (Recordatorios) inputStream.readObject();
            }
        } catch (IOException ex) {
            System.err.println("Error al leer los recordatorios");
            System.err.println(ex.getMessage());
            ex.printStackTrace();
        } catch (ClassNotFoundException cnfe) {
            System.err.println("Error al leer los recordatorios");
            System.err.println(cnfe.getMessage());
        }

        return recordatorios;
    }
}
